#pragma once
#include <tchar.h>
#include <dshow.h>
#include <shellapi.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>

#include <d3d9.h>
#include <vmr9.h>

#include "smartptr.h"
#include "dshowutil.h"

#include <stack>

#include "Clip.h"

class ClipPlayer
{
public:
	ClipPlayer(void);
	~ClipPlayer(void);

	bool initialise(HINSTANCE hInst);
	void destroy(void);

	void pushClip(Clip* pClip, bool clear);
	bool beginPlayback(const std::string& videoFile);
	void update(void);

	void setNewClipCallback(void(*pCallback)(CLIP_TYPE,CLIP_TYPE));
	void setClipStackEmptyCallback(void(*pCallback)(void));
private:
	bool hasVMR9(void);
	static LRESULT CALLBACK MsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
	void playNextClip(void);
	void seekToPosition(REFTIME time);
	double getPosition(void);

	HWND mHwnd;
	IGraphBuilder* mpGraphBuilder;
	IMediaEventEx* mpMediaEvent;
	IMediaControl* mpMediaControl;
	IMediaSeeking* mpMediaSeeking;
	IVMRWindowlessControl9* mpWindowlessControl;
	RECT mRenderTarget;

	std::stack<Clip*> mClips;
	Clip* mpCurrentClip;

	void(*mfNewClipCallback)(CLIP_TYPE,CLIP_TYPE);
	void(*mfClipStackEmptyCallback)(void);
};
